<?php

namespace app\xiangji\services;


use app\common\caches\DiaryLabelCache;
use app\common\facades\auth\Auth;
use app\common\models\LoveDiaryLabel;

class LoveDiaryLabelService
{
    const SYSTEM_LABEL_USERID = 0;

    private $user;
    private $labelModel;

    public function __construct(LoveDiaryLabel $labelModel)
    {
        $this->user = Auth::guard('api')->user();
        $this->labelModel = $labelModel;
    }

    public function fetch()
    {
        $data = DiaryLabelCache::getFetchList($this->user->id);
        if (!$data) {
            $data = $this->labelModel->whereIn('user_id', [self::SYSTEM_LABEL_USERID, $this->user->id])->field(['id', 'category', 'name', 'type'])->select();
            DiaryLabelCache::setFetchList($this->user->id, $data);
        }
        return $data;
    }
}
